//
//  PhotoViewController.m
//  PhotoWallDemo
//
//  Created by CZH on 15/9/23.
//  Copyright (c) 2015年 CZH. All rights reserved.
//

#import "PhotoViewController.h"

@interface PhotoViewController ()<UIScrollViewDelegate>

@end

@implementation PhotoViewController
{
    NSArray * patharray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置导航和tabbar的偏移修正为no
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self analyzplist];
}
//创建scrollview
- (void)analyzplist
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height ;
    NSMutableArray *x = [NSMutableArray arrayWithArray:[self.imageStr componentsSeparatedByString:@","]];
    [x removeObjectAtIndex:0];
    NSLog(@"hhhhhhhhhhh%@",x);
    //创建滚动视图
    UIScrollView * sv = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    //设置滚动视图包含的size
    sv.contentSize = CGSizeMake((x.count)*width, height);
    sv.tag = 100;
    sv.bounces = NO;
    sv.showsHorizontalScrollIndicator = NO;
    sv.showsVerticalScrollIndicator = NO;
    //sv.backgroundColor = [UIColor redColor];
    //创建图片视图
    for (int i = 0; i<x.count; i++)
    {

        
        //添加小的scrollview
        UIScrollView * small = [[UIScrollView alloc]initWithFrame:CGRectMake(width*(i), 0, width, height)];
        small.minimumZoomScale = 1.0;
        small.maximumZoomScale = 2.0;
        small.delegate = self;
        small.tag = 200;
        [sv addSubview:small];
        
        //设置图片视图
        UIImageView * iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        [iv sd_setImageWithURL:[NSURL URLWithString:x[i]]placeholderImage:nil];        //设置填充状态
        iv.contentMode = UIViewContentModeScaleAspectFit;
        //图片添加到滚动视图上
        [small addSubview:iv];
        //打开交互
        iv.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclick:)];
        [iv addGestureRecognizer:tap];
    }
    //设置按页翻动
    sv.pagingEnabled = YES;
    //设置起始的偏移量（可以找到当前页）
    sv.contentOffset = CGPointMake(width*(_index - 1), 0);
    sv.delegate = self;
    //把滚动视图添加到屏幕上
    [self.view addSubview:sv];
    self.navigationItem.title = [NSString stringWithFormat:@"%ld/%ld",_index,x.count];
}
//当滚动视图开始滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.tag == 200) {
        return;
    }
    NSInteger page = scrollView.contentOffset.x/self.view.bounds.size.width;
    self.navigationItem.title = [NSString stringWithFormat:@"%ld/%ld",page + 1,[self.imageStr componentsSeparatedByString:@","].count - 1];
}
//点击手势的方法
- (void)onclick:(UITapGestureRecognizer*)sender
{
    if (self.navigationController.navigationBarHidden == NO) {
        //隐藏导航栏
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        //隐藏状态栏
        [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }else{
        //显示导航栏
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        //显示状态栏
        [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    }
}
//将要被滑动，指大的滚动视图
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //小scrollview视图不操作
    if (scrollView.tag == 200) {
        return;
    }
    //找到当前显示的字视图
    UIScrollView * subview = scrollView.subviews[self.index];
    //设置当前的缩放大小
    subview.zoomScale = 1.0;
}
//返回被缩放的子视图
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    //大的视图不需要返回缩放视图
    if (scrollView.tag == 100) {
        return nil;
    }
    //小的scrollview上的imageview
    return scrollView.subviews[0];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
